<?php

namespace control;
/*
*稽核记录明细类
*/


class inspectionDetailClass extends adIndexClass
{
    private const title = '稽核记录明细';
    private const page  = 'inspectionDetail';
    private const table = 'inspection_detail';

    /**
     * 字段初始化
     */
    public function fieldInit($fun): array
    {
        $tr = [
            'recordDate'  => [
                'name'  => '稽核日期',
                'width' => 100,
                'sort'  => 1,
            ],
            'orderNo'     => [
                'name'  => "单据编号",
                'width' => 100,
                'sort'  => 1,
            ],
            'level'       => [
                'name'  => "稽核级别",
                'width' => 100,
                'sort'  => 1,
            ],
            'target_name' => [
                'name'  => '受稽核对象',
                'width' => 100,
                'sort'  => 1,
            ],
            'type'        => [
                'name'  => '稽核类别',
                'width' => 100,
                'sort'  => 1,
            ],
            'audit_unit'  => [
                'name'  => '稽核组长/稽核单位',
                'width' => 150,
                'sort'  => 1,
            ],
            'checkTime'   => [
                'name'  => '稽核时间',
                'width' => 100,
            ],
            'checkStid'   => [
                'name'  => '稽核员',
                'width' => 100,
            ],
            'item'        => [
                'name'  => '稽核项目',
                'width' => 100,
                'sort'  => 1,
            ],
            'basis'       => [
                'name'  => '稽核依据',
                'width' => 100,
                'sort'  => 1,
            ],
            'results'     => [
                'name'  => '稽核结果',
                'width' => 150,
                'sort'  => 1,
            ],
            'text'        => [
                'name'  => '存在问题',
                'width' => 150,
                'sort'  => 1,
            ]
        ];
        switch ($fun) {
            case 'notifiedSearch':
                $tr = array_merge($tr, [
                    'noticeDate'    => [
                        'name'  => '通知整改日期',
                        'width' => 100,
                        'sort'  => 1,
                    ],
                    'person_liable' => [
                        'name'  => "整改责任人",
                        'width' => 100,
                    ],
                    'state'         => [
                        'name'  => "状态",
                        'width' => 100,
                    ]
                ]);
                break;
            case 'fedBackSearch':
                $tr = array_merge($tr, [
                    'measure'       => [
                        'name'  => '整改措施',
                        'width' => 200,
                        'sort'  => 1,
                    ],
                    'noticeDate'    => [
                        'name'  => "通知整改日期",
                        'width' => 100,
                        'sort'  => 1,
                    ],
                    'person_liable' => [
                        'name'  => "整改责任人",
                        'width' => 100,
                    ],
                    'feedback_date' => [
                        'name'  => "反馈措施日期",
                        'width' => 100,
                        'sort'  => 1,
                    ],
                    'state'         => [
                        'name'  => "状态",
                        'width' => 100,
                    ]
                ]);
                break;
            case 'passedSearch':
            case 'failSearch':
                $tr = array_merge($tr, [
                    'noticeDate'        => [
                        'name'  => '通知整改日期',
                        'width' => 100,
                        'sort'  => 1,
                    ],
                    'person_liable'     => [
                        'name'  => "整改责任人",
                        'width' => 100,
                    ],
                    'measure'           => [
                        'name'  => "整改措施",
                        'width' => 200,
                    ],
                    'feedback_date'     => [
                        'name'  => "反馈措施日期",
                        'width' => 100,
                        'sort'  => 1,
                    ],
                    'result'            => [
                        'name'  => "整改结果",
                        'width' => 200,
                    ],
                    'completion'        => [
                        'name'  => "完成情况",
                        'width' => 200,
                        'sort'  => 1,
                    ],
                    'rectification_num' => [
                        'name'  => "整改次数",
                        'width' => 100,
                        'sort'  => 1,
                    ],
                    'verify_stid'       => [
                        'name'  => "整改验证人",
                        'width' => 100,
                    ],
                    'verify_date'       => [
                        'name'  => "整改验证日期",
                        'width' => 100,
                        'sort'  => 1,
                    ],
                    'state'             => [
                        'name'  => "状态",
                        'width' => 200,
                    ]
                ]);
                break;
            case 'closeSearch':
            case 'allSearch':
                $tr = array_merge($tr, [
                    'noticeDate'        => [
                        'name'  => '通知整改日期',
                        'width' => 100,
                        'sort'  => 1,
                    ],
                    'person_liable'     => [
                        'name'  => "整改责任人",
                        'width' => 100,
                    ],
                    'measure'           => [
                        'name'  => "整改措施",
                        'width' => 100,
                    ],
                    'feedback_date'     => [
                        'name'  => "反馈措施日期",
                        'width' => 100,
                        'sort'  => 1,
                    ],
                    'result'            => [
                        'name'  => "整改结果",
                        'width' => 100,
                    ],
                    'completion'        => [
                        'name'  => "完成情况",
                        'width' => 100,
                        'sort'  => 1,
                    ],
                    'rectification_num' => [
                        'name'  => "整改次数",
                        'width' => 100,
                        'sort'  => 1,
                    ],
                    'verify_stid'       => [
                        'name'  => "整改验证人",
                        'width' => 100,
                    ],
                    'verify_date'       => [
                        'name'  => "整改验证日期",
                        'width' => 100,
                        'sort'  => 1,
                    ],
                    'close_date'        => [
                        'name'  => "关闭日期",
                        'width' => 100,
                        'sort'  => 1,
                    ],
                    'close_stid'        => [
                        'name'  => "关闭人",
                        'width' => 100,
                    ],
                    'state'             => [
                        'name'  => "状态",
                        'width' => 100,
                    ]
                ]);
                break;
            default:
                $tr = array_merge($tr, [
                    'state' => [
                        'name'  => "状态",
                        'width' => 100,
                    ]
                ]);
        }
        return $tr;
    }

    /**
     * 查询表单
     *
     * @param string $workFlow
     * @return string
     */
    public function homeForm(string $workFlow): string
    {
        //生成查询表单
        switch ($workFlow) {
            case '待通知':
                $word = '稽核';
                break;
            case '已通知':
                $word = '通知整改';
                break;
            case '已反馈':
                $word = '反馈措施';
                break;
            case '验证通过':
            case '验证未过':
                $word = '整改验证';
                break;
            case '已关闭':
                $word = '关闭';
                break;
            default:
                $word = '稽核';
                break;
        }
        $array = [
            [
                [
                    "单据编号"  => text(array("name" => "orderNo")) . hidden(array("name" => "workFlow", "value" => $workFlow)),
                    "稽核级别"  => select(array("name" => "level", "option" => [1 => '内部', 2 => '外部'])),
                    '受稽核对象' => text(array("name" => "target_name")),
                ],
                [
                    '稽核类别'      => select(array("name" => "type", "option" => explode('，', para('inspectionType')))),
                    "稽核组长/稽核单位" => text(array("name" => "audit_unit")),
                    '稽核员'       => selectMultiple(array("name" => "checkStid", "table" => "staff", 'sqlField' => 'name', 'sqlId' => 'stid', 'class' => 'width200')),
                ],
                [
                    $word . "日期" => text(array("name" => "recordDate", 'laydate' => ['type' => 'date', 'range' => '~'])),
                ]
            ]
        ];
        if ($workFlow == '全部') {
            $array[0][2]['稽核结果'] = select(array("name" => "result", "option" => [1 => '合格', 2 => '不合格']));
        }
        if ($workFlow != '待通知') {
            $array[0][2]['整改责任人'] = selectMultiple(array("name" => "person_liable", "table" => "staff", 'sqlField' => 'name', 'sqlId' => 'stid', 'class' => 'width200'));
        }
        return $this->tableShow($array, 1);
    }

    /*列表页-查询条件*/
    public function homeOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->homeForm('待通知');
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    /*稽核记录明细-查询条件*/
    public function home(): string
    {
        $html = $this->homeOptions(false);
        //控件
        $button = "";
        if ($this->power(self::page, "edit")) {
            $button .= "<span onclick=\"rectification()\" class='spanButton'>整改通知</span>\n";
        }
        if ($this->power(self::page, "export")) {
            $button .= "<span class='spanButton' onclick=\"layer('importTemplate','exportFrame',{cla:'" . self::page . "'})\">导出</span>";
        }
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
    }

    /*客户列表-查询结果*/
    public function homeSearch(): bool|string
    {
        //like查询数组
        $like = array(
            "b.orderNo"     => $this->post('orderNo'),
            "b.level"       => $this->post('level'),
            "b.target_name" => $this->post('target_name'),
            "b.type"        => $this->post('type'),
            "b.audit_unit"  => $this->post('audit_unit'),
            "a.checkStid"   => $this->post('checkStid'),//稽核员
        );
        //全等查询数组
        $equal = array(
            'a.results' => 2,
            'b.state'   => '已通过',
        );

        $and = "";
        if ($this->post('recordDate')) {
            $and .= self::timeComparison($this->post('recordDate'), 'date', 'b.recordDate');
        }
        $and .= " and c.id is null ";
        //排序
        $orderBy = $this->listSort(array(
            "search" => $this->post('list_sort_key') ?: 'a.time',
            "sort"   => $this->post('list_sort') ?: "desc",
            "array"  => array(),
        ));
        //获取查询结果
        $where                       = search(array("like" => $like, "=" => $equal)) . $and;
        $page                        = $this->page(array(
            "table" => " inspection_detail as a left join inspection as b on a.orderId = b.id left join rectification_detail as c on c.detailId = a.id ",
            "where" => $where,
            "field" => " b.recordDate,b.orderNo,b.level,b.target_name,b.type,b.audit_unit,a.* ",
            "order" => $orderBy['orderBy']
        ));
        $_SESSION[self::page]['sql'] = $page['fullSql'];
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        $inspectionClass = new inspectionClass();
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate   = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case "level":
                        $row[] = ($array['level'] == 1 ? '内部' : '外部');
                        break;
                    case "checkTime":
                        $row[] = $inspectionClass->timeAssembly($array['checkTime'], $array['checkTimeEnd'], '至');
                        break;
                    case "checkStid":
                        $row[] = self::findStaff($array['checkStid']);
                        break;
                    case "results":
                        $row[] = ($array['results'] == 1 ? '合格' : '不合格');
                        break;
                    case "state":
                        $row[] = '待通知';
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        //控件
        $button = "";
        //生成页面
        $li = $this->li(array(
            "body"        => $body,
            "install"     => $installSearch,
            "button"      => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page"        => $page['para'],
        ));
        //返回
        $json['html'] = $page['hint'] . $button . "<form name='listForm'>" . $li . "</form>" . $page['button'] . clear;
        $json['html'] .= <<<eof
<script>
function rectification(e){
     $.post(root+'control/adIndex.html?cla={$this->get('cla')}&fun=rectification',$("[name='listForm']").serialize(),function(res) {
            if(res.warn === 2){
                if (res.href) {
                    window.location.href = res.href;
                }
            }else{
                warn(res.warn)
            }
       },'json')
}
</script>
eof;
        return json_encode($json, 320);
    }

    /**
     * 整改通知验证
     *
     */
    public function rectification(): bool|string
    {
        $listId = $this->post('listId');
        if (static::isNull($listId)) {
            $json['warn'] = "你一个数据都没有选择呢";
        } else {
            $target = '';
            $ids    = '';
            foreach ((array)$listId as $k => $id) {
                $inspection_detail = query("inspection_detail", " id = '$id' ");
                $inspection        = query("inspection", " id = '" . $inspection_detail['orderId'] . "' ");
                if ($k == 0) {
                    $target = $inspection['target'];
                }
                if ($inspection['target'] != $target) {
                    $json['warn'] = "请选择同一个受稽核对象";
                    goto resp;
                }
                $ids .= $inspection_detail['id'] . ',';
            }
            $ids          = rtrim($ids, ',');
            $json['warn'] = 2;
            $json['href'] = root . "control/adIndex.html?cla=rectification&fun=edit&listId=" . $ids;
        }
        resp:
        return json_encode($json, 320);
    }


    /**
     * 导出
     */
    public function exportData()
    {
        $templateId     = $this->get('templateId');
        $checkedId      = $this->get('checkedId') ? explode(',', $this->get('checkedId')) : [];
        if (!$this->power(self::page, 'export')) {
            $_SESSION['warn'] = '你还没有导出的权限';
        } else {
            $data            = [];
            $inspectionClass = new inspectionClass();
            /***********标题**********/
            $title = [];
            $tempFieldAll = importTemplateClass::templateField(self::page, 'exportFieldInit', id:$templateId);
            foreach ($tempFieldAll as $array) {
                if ($array['display']) {
                    $title[] = $array['aliasName'];
                }
            }
            //判断是否选中导出
            if (!self::is_null_array($checkedId)) {
                $result = find(array(
                    "table" => " inspection_detail as a left join inspection as b on a.orderId = b.id left join rectification_detail as c on c.detailId = a.id ",
                    "field" => " b.recordDate,b.orderNo,b.level,b.target_name,b.type,b.audit_unit,a.* ",
                    "where" => "a.id in ('" . implode("','", $checkedId) . "') order by a.time desc",
                ))['array'];
            } else {
                $sql    = $_SESSION[self::page]['sql'];
                $result = sqlFind($sql);
            }
            //导出数据生成
            foreach ($result as $array) {
                $column = [];
                foreach ($tempFieldAll as $templateField) {
                    if ($templateField['display']) {
                        $column[] = match ($templateField['columnName']) {
                            '稽核日期' => $array['recordDate'],
                            '单据编号' => $array['orderNo'],
                            '稽核级别' => ($array['level'] == 1 ? '内部' : '外部'),
                            '受稽核对象' => $array['target_name'],
                            '稽核类别' => $array['type'],
                            '稽核组长/稽核单位' => $array['audit_unit'],
                            '稽核时间' => $inspectionClass->timeAssembly($array['checkTime'], $array['checkTimeEnd'], '至'),
                            '稽核员' => self::findStaff($array['checkStid']),
                            '稽核项目' => $array['item'],
                            '稽核依据' => $array['basis'],
                            '稽核结果' => ($array['results'] == 1 ? '合格' : '不合格'),
                            '存在问题' => $array['text'],
                            '状态' => '待通知',
                            default => '',
                        };
                    }
                }
                $data[] = $column;
            }
            $text = $this->control['name'] . "导出了待通知稽核记录明细";
            $this->logText(["type" => self::page, "text" => $text]);
            $this->excelOut($title, $data, "待通知稽核记录明细.xlsx");
            exit(0);
        }
        header("Location:" . getenv("HTTP_REFERER"));
    }

    /*列表页-查询条件*/
    public function notifiedOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->homeForm('已通知');
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    /**
     * 已通知
     *
     */
    public function notified(): bool|string
    {
        $html = $this->notifiedOptions(false);
        //控件
        $button = "";
        if ($this->power('inspectionNotified', "export")) {
            $button .= "<span class='spanButton' onclick=\"layer('importTemplate','exportFrame',{cla:'" . self::page . "',exportFun:'exportCommonData',workFlow:'已通知',classify:'已通知'})\">导出</span>";
        }
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
    }

    /**
     * 已通知
     * @return bool|string
     */
    public function notifiedSearch(): bool|string
    {
        return $this->commonSearch();
    }

    /*列表页-查询条件*/
    public function fedBackOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->homeForm('已反馈');
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    /**
     * 已通知
     *
     */
    public function fedBack(): bool|string
    {
        $html = $this->fedBackOptions(false);
        //控件
        $button = "";
        if ($this->power('inspectionFedBack', "export")) {
            $button .= "<span class='spanButton' onclick=\"layer('importTemplate','exportFrame',{cla:'" . self::page . "',exportFun:'exportCommonData',workFlow:'已反馈',classify:'已反馈'})\">导出</span>";
        }
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
    }

    /**
     * 已反馈
     * @return bool|string
     */
    public function fedBackSearch(): bool|string
    {
        return $this->commonSearch();
    }

    /*列表页-查询条件*/
    public function passedOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->homeForm('验证通过');
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    /**
     * 已通知
     *
     */
    public function passed(): bool|string
    {
        $html = $this->passedOptions(false);
        //控件
        $button = "";
        if ($this->power('inspectionPassed', "export")) {
            $button .= "<span class='spanButton' onclick=\"layer('importTemplate','exportFrame',{cla:'" . self::page . "',exportFun:'exportCommonData',workFlow:'验证通过',classify:'验证通过'})\">导出</span>";
        }
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
    }

    /**
     * 验证通过
     * @return bool|string
     */
    public function passedSearch(): bool|string
    {
        return $this->commonSearch();
    }

    /*列表页-查询条件*/
    public function failOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->homeForm('验证未过');
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    /**
     * 已通知
     *
     */
    public function fail(): bool|string
    {
        $html = $this->failOptions(false);
        //控件
        $button = "";
        if ($this->power('inspectionFail', "export")) {
            $button .= "<span class='spanButton' onclick=\"layer('importTemplate','exportFrame',{cla:'" . self::page . "',exportFun:'exportCommonData',workFlow:'验证未过',classify:'验证未过'})\">导出</span>";
        }
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
    }

    /**
     * 验证未过
     * @return bool|string
     */
    public function failSearch(): bool|string
    {
        return $this->commonSearch();
    }

    /*列表页-查询条件*/
    public function closeOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->homeForm('已关闭');
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    /**
     * 已通知
     *
     */
    public function close(): bool|string
    {
        $html = $this->closeOptions(false);
        //控件
        $button = "";
        if ($this->power('inspectionClose', "export")) {
            $button .= "<span class='spanButton' onclick=\"layer('importTemplate','exportFrame',{cla:'" . self::page . "',exportFun:'exportCommonData',workFlow:'已关闭',classify:'已关闭'})\">导出</span>";
        }
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
    }

    /**
     * 已关闭
     * @return bool|string
     */
    public function closeSearch(): bool|string
    {
        return $this->commonSearch();
    }

    /*列表页-查询条件*/
    public function allOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->homeForm('全部');
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    /**
     * 已通知
     *
     */
    public function all(): bool|string
    {
        $html = $this->allOptions(false);
        //控件
        $button = "";
        if ($this->power('inspectionAll', "export")) {
            $button .= "<span class='spanButton' onclick=\"layer('importTemplate','exportFrame',{cla:'" . self::page . "',exportFun:'exportCommonData',workFlow:'全部',classify:'全部'})\">导出</span>";
        }
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
    }

    /**
     * 全部
     * @return bool|string
     */
    public function allSearch(): bool|string
    {
        return $this->commonSearch();
    }

    /**
     * 公共列表
     * @return bool|string
     */
    public function commonSearch(): bool|string
    {
        //like查询数组
        $like = array(
            "b.orderNo"     => $this->post('orderNo'),
            "b.level"       => $this->post('level'),
            "b.target_name" => $this->post('target_name'),
            "b.type"        => $this->post('type'),
            "b.audit_unit"  => $this->post('audit_unit'),
            "a.checkStid"   => $this->post('checkStid'),//稽核员
        );
        //全等查询数组
        $equal    = array(
            'b.state'         => '已通过',
            'c.person_liable' => $this->post('person_liable'),//整改责任人
        );
        $and      = "";
        $workFlow = $this->post('workFlow') ?: '已通知';
        if ($workFlow == '全部') {
            if ($this->post('result')) {
                $and .= " and a.results = '" . $this->post('result') . "' ";
            }
        } else {
            $and .= " and a.results = 2 ";  //全部展示合格的
        }
        switch ($workFlow) {
            case '已通知':
                $and .= " and c.state = 0 ";
                if ($this->post('recordDate')) {
                    $and .= self::timeComparison($this->post('recordDate'), 'date', 'c.recordDate');
                }
                break;
            case '已反馈':
                $and .= " and c.state = 1 ";
                if ($this->post('recordDate')) {
                    $and .= self::timeComparison($this->post('recordDate'), 'date', 'c.feedback_date');
                }
                break;
            case '验证通过':
                $and .= " and c.state = 2 and c.completion = 1 ";
                if ($this->post('recordDate')) {
                    $and .= self::timeComparison($this->post('recordDate'), 'date', 'c.verify_date');
                }
                break;
            case '验证未过':
                $and .= " and c.state = 2 and c.completion = 0 ";
                if ($this->post('recordDate')) {
                    $and .= self::timeComparison($this->post('recordDate'), 'date', 'c.verify_date');
                }
                break;
            case '已关闭':
                $and .= " and c.state = 3 ";
                if ($this->post('recordDate')) {
                    $and .= self::timeComparison($this->post('recordDate'), 'date', 'c.close_date');
                }
                break;
            case '全部':
                if ($this->post('recordDate')) {
                    $and .= self::timeComparison($this->post('recordDate'), 'date', 'b.recordDate');
                }
                break;
        }
        //排序
        $orderBy = $this->listSort(array(
            "search" => $this->post('list_sort_key') ?: 'a.time',
            "sort"   => $this->post('list_sort') ?: "desc",
            "array"  => array(),
        ));
        //获取查询结果
        $where                      = search(array("like" => $like, "=" => $equal)) . $and;
        $page                       = $this->page(array(
            "table" => " inspection_detail as a left join inspection as b on a.orderId = b.id left join rectification_detail as c on c.detailId = a.id ",
            "where" => $where,
            "field" => " b.recordDate,b.orderNo,b.level,b.target_name,b.type,b.audit_unit,a.*,c.person_liable,c.recordDate as noticeDate,c.measure,c.result,c.completion,c.feedback_date,c.feedback_stid,c.verify_date,c.verify_stid,c.rectification_num,c.close_date,c.close_stid,c.state ",
            "order" => $orderBy['orderBy']
        ));
        $_SESSION[$workFlow]['sql'] = $page['fullSql'];
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton        = array(
            $this->checkAll("listForm", "listId")
        );
        $inspectionClass = new inspectionClass();
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $state    = '待通知';
            if ($array['state'] != '') {
                switch ($array['state']) {
                    case 0:
                        $state = '已通知';
                        break;
                    case 1:
                        $state = '已反馈';
                        break;
                    case 2:
                        $state = '已验证';
                        break;
                    case 3:
                        $state = '已关闭';
                        break;
                }
            }
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case "level":
                        $row[] = ($array['level'] == 1 ? '内部' : '外部');
                        break;
                    case "checkTime":
                        $row[] = $inspectionClass->timeAssembly($array['checkTime'], $array['checkTimeEnd'], '至');
                        break;
                    case "person_liable":
                    case "checkStid":
                    case "verify_stid":
                    case "close_stid":
                        $row[] = (empty($array[$key]) ? '' : self::findStaff($array[$key]));
                        break;
                    case "results":
                        $row[] = ($array['results'] == 1 ? '合格' : '不合格');
                        break;
                    case "completion":
                        $row[] = ($array['completion'] == 1 ? '已完成' : '未完成');
                        break;
                    case "state":
                        $row[] = $state;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        //控件
        $button = "";
        //生成页面
        $li = $this->li(array(
            "body"        => $body,
            "install"     => $installSearch,
            "button"      => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page"        => $page['para'],
        ));
        //返回
        $json['html'] = $page['hint'] . $button . "<form name='listForm'>" . $li . "</form>" . $page['button'] . clear;
        return json_encode($json, 320);
    }

    /**
     * 导出
     */
    public function exportCommonData()
    {
        $templateId     = $this->get('templateId');
        $checkedId      = $this->get('checkedId') ? explode(',', $this->get('checkedId')) : [];
        $workFlow       = $this->get('workFlow');
        $power          = self::page;
        switch ($workFlow) {
            case '已通知':
                $power = 'inspectionNotified';
                break;
            case '已反馈':
                $power = 'inspectionFedBack';
                break;
            case '验证通过':
                $power = 'inspectionPassed';
                break;
            case '验证未过':
                $power = 'inspectionFail';
                break;
            case '已关闭':
                $power = 'inspectionClose';
                break;
            case '全部':
                $power = 'inspectionAll';
                break;
        }
        if (!$this->power($power, 'export')) {
            $_SESSION['warn'] = '你还没有导出的权限';
        } else {
            $inspectionClass = new inspectionClass();
            $data            = [];
            $sql             = $_SESSION[$workFlow]['sql'];
            /***********标题**********/
            $title = [];
            $tempFieldAll = importTemplateClass::templateField(self::page, 'exportFieldInit', id:$templateId);
            foreach ($tempFieldAll as $array) {
                if ($array['display']) {
                    $title[] = $array['aliasName'];
                }
            }
            //判断是否选中导出
            if (!self::is_null_array($checkedId)) {
                $result = find(array(
                    "field" => " b.recordDate,b.orderNo,b.level,b.target_name,b.type,b.audit_unit,a.*,c.person_liable,c.recordDate as noticeDate,c.measure,c.result,c.completion,c.feedback_date,c.feedback_stid,c.verify_date,c.verify_stid,c.rectification_num,c.close_date,c.close_stid,c.state ",
                    "table" => " inspection_detail as a left join inspection as b on a.orderId = b.id left join rectification_detail as c on c.detailId = a.id ",
                    "where" => " a.id in ('" . implode("','", $checkedId) . "') order by a.time desc",
                ))['array'];
            } else {
                $result = sqlFind($sql);
            }
            switch ($workFlow) {
                case '已通知':
                    //导出数据生成
                    foreach ($result as $array) {
                        $column = [];
                        $state  = '待通知';
                        if ($array['state'] != '') {
                            switch ($array['state']) {
                                case 0:
                                    $state = '已通知';
                                    break;
                                case 1:
                                    $state = '已反馈';
                                    break;
                                case 2:
                                    $state = '已验证';
                                    break;
                                case 3:
                                    $state = '已关闭';
                                    break;
                            }
                        }
                        foreach ($tempFieldAll as $templateField) {
                            if ($templateField['display']) {
                                $column[] = match ($templateField['columnName']) {
                                    '稽核日期' => $array['recordDate'],
                                    '单据编号' => $array['orderNo'],
                                    '稽核级别' => ($array['level'] == 1 ? '内部' : '外部'),
                                    '受稽核对象' => $array['target_name'],
                                    '稽核类别' => $array['type'],
                                    '稽核组长/稽核单位' => $array['audit_unit'],
                                    '稽核时间' => $inspectionClass->timeAssembly($array['checkTime'], $array['checkTimeEnd'], '至'),
                                    '稽核员' => self::findStaff($array['checkStid']),
                                    '稽核项目' => $array['item'],
                                    '稽核依据' => $array['basis'],
                                    '稽核结果' => ($array['results'] == 1 ? '合格' : '不合格'),
                                    '存在问题' => $array['text'],
                                    '通知整改日期' => $array['noticeDate'],
                                    '整改责任人' => (empty($array['person_liable']) ? '' : self::findStaff($array['person_liable'])),
                                    '状态' => $state,
                                    default => '',
                                };
                            }
                        }
                        $data[] = $column;
                    }
                    break;
                case '已反馈':
                    //导出数据生成
                    foreach ($result as $array) {
                        $column = [];
                        $state  = '待通知';
                        if ($array['state'] != '') {
                            switch ($array['state']) {
                                case 0:
                                    $state = '已通知';
                                    break;
                                case 1:
                                    $state = '已反馈';
                                    break;
                                case 2:
                                    $state = '已验证';
                                    break;
                                case 3:
                                    $state = '已关闭';
                                    break;
                            }
                        }
                        foreach ($all['array'] as $templateField) {
                            if ($templateField['display']) {
                                $column[] = match ($templateField['columnName']) {
                                    '稽核日期' => $array['recordDate'],
                                    '单据编号' => $array['orderNo'],
                                    '稽核级别' => ($array['level'] == 1 ? '内部' : '外部'),
                                    '受稽核对象' => $array['target_name'],
                                    '稽核类别' => $array['type'],
                                    '稽核组长/稽核单位' => $array['audit_unit'],
                                    '稽核时间' => $inspectionClass->timeAssembly($array['checkTime'], $array['checkTimeEnd'], '至'),
                                    '稽核员' => self::findStaff($array['checkStid']),
                                    '稽核项目' => $array['item'],
                                    '稽核依据' => $array['basis'],
                                    '稽核结果' => ($array['results'] == 1 ? '合格' : '不合格'),
                                    '存在问题' => $array['text'],
                                    '整改措施' => $array['measure'],
                                    '通知整改日期' => $array['noticeDate'],
                                    '整改责任人' => (empty($array['person_liable']) ? '' : self::findStaff($array['person_liable'])),
                                    '反馈措施日期' => $array['feedback_date'],
                                    '状态' => $state,
                                    default => '',
                                };
                            }
                        }
                        $data[] = $column;
                    }
                    break;
                case '验证通过':
                case '验证未过':
                    //导出数据生成
                    foreach ($result as $array) {
                        $column = [];
                        $state  = '待通知';
                        if ($array['state'] != '') {
                            switch ($array['state']) {
                                case 0:
                                    $state = '已通知';
                                    break;
                                case 1:
                                    $state = '已反馈';
                                    break;
                                case 2:
                                    $state = '已验证';
                                    break;
                                case 3:
                                    $state = '已关闭';
                                    break;
                            }
                        }
                        foreach ($all['array'] as $templateField) {
                            if ($templateField['display']) {
                                $column[] = match ($templateField['columnName']) {
                                    '稽核日期' => $array['recordDate'],
                                    '单据编号' => $array['orderNo'],
                                    '稽核级别' => ($array['level'] == 1 ? '内部' : '外部'),
                                    '受稽核对象' => $array['target_name'],
                                    '稽核类别' => $array['type'],
                                    '稽核组长/稽核单位' => $array['audit_unit'],
                                    '稽核时间' => $inspectionClass->timeAssembly($array['checkTime'], $array['checkTimeEnd'], '至'),
                                    '稽核员' => self::findStaff($array['checkStid']),
                                    '稽核项目' => $array['item'],
                                    '稽核依据' => $array['basis'],
                                    '稽核结果' => ($array['results'] == 1 ? '合格' : '不合格'),
                                    '存在问题' => $array['text'],
                                    '通知整改日期' => $array['noticeDate'],
                                    '整改责任人' => (empty($array['person_liable']) ? '' : self::findStaff($array['person_liable'])),
                                    '整改措施' => $array['measure'],
                                    '反馈措施日期' => $array['feedback_date'],
                                    '整改结果' => $array['result'],
                                    '完成情况' => ($array['completion'] == 1 ? '已完成' : '未完成'),
                                    '整改次数' => $array['rectification_num'],
                                    '整改验证人' => self::findStaff($array['verify_stid']),
                                    '整改验证日期' => $array['verify_date'],
                                    '状态' => $state,
                                    default => '',
                                };
                            }
                        }
                        $data[] = $column;
                    }
                    break;
                case '已关闭':
                case '全部':
                    //导出数据生成
                    foreach ($result as $array) {
                        $column = [];
                        $state  = '待通知';
                        if ($array['state'] != '') {
                            switch ($array['state']) {
                                case 0:
                                    $state = '已通知';
                                    break;
                                case 1:
                                    $state = '已反馈';
                                    break;
                                case 2:
                                    $state = '已验证';
                                    break;
                                case 3:
                                    $state = '已关闭';
                                    break;
                            }
                        }
                        foreach ($all['array'] as $templateField) {
                            if ($templateField['display']) {
                                $column[] = match ($templateField['columnName']) {
                                    '稽核日期' => $array['recordDate'],
                                    '单据编号' => $array['orderNo'],
                                    '稽核级别' => ($array['level'] == 1 ? '内部' : '外部'),
                                    '受稽核对象' => $array['target_name'],
                                    '稽核类别' => $array['type'],
                                    '稽核组长/稽核单位' => $array['audit_unit'],
                                    '稽核时间' => $inspectionClass->timeAssembly($array['checkTime'], $array['checkTimeEnd'], '至'),
                                    '稽核员' => self::findStaff($array['checkStid']),
                                    '稽核项目' => $array['item'],
                                    '稽核依据' => $array['basis'],
                                    '稽核结果' => ($array['results'] == 1 ? '合格' : '不合格'),
                                    '存在问题' => $array['text'],
                                    '通知整改日期' => $array['noticeDate'],
                                    '整改责任人' => (empty($array['person_liable']) ? '' : self::findStaff($array['person_liable'])),
                                    '整改措施' => $array['measure'],
                                    '反馈措施日期' => $array['feedback_date'],
                                    '整改结果' => $array['result'],
                                    '完成情况' => ($array['completion'] == 1 ? '已完成' : '未完成'),
                                    '整改次数' => $array['rectification_num'],
                                    '整改验证人' => self::findStaff($array['verify_stid']),
                                    '整改验证日期' => $array['verify_date'],
                                    '关闭日期' => $array['close_date'],
                                    '关闭人' => self::findStaff($array['close_stid']),
                                    '状态' => $state,
                                    default => '',
                                };
                            }
                        }
                        $data[] = $column;
                    }
                    break;
            }
            $text = $this->control['name'] . "导出了{$workFlow}稽核记录明细";
            $this->logText(["type" => $power, "text" => $text]);
            $this->excelOut($title, $data, $workFlow . "稽核记录明细.xlsx");
            exit(0);
        }
        header("Location:" . getenv("HTTP_REFERER"));
    }

    /**
     * 导出模板字段初始化
     * @return array[]
     */
    public function exportFieldInit($classify = ''): array
    {
        return match ($classify) {
            '全部', '已关闭' => [
                [
                    'columnName' => '稽核日期',
                    'aliasName'  => '稽核日期',
                ],
                [
                    'columnName' => '单据编号',
                    'aliasName'  => '单据编号',
                ],
                [
                    'columnName' => '稽核级别',
                    'aliasName'  => '稽核级别',
                ],
                [
                    'columnName' => '受稽核对象',
                    'aliasName'  => '受稽核对象',
                ],
                [
                    'columnName' => '稽核类别',
                    'aliasName'  => '稽核类别',
                ],
                [
                    'columnName' => '稽核组长/稽核单位',
                    'aliasName'  => '稽核组长/稽核单位',
                ],
                [
                    'columnName' => '稽核时间',
                    'aliasName'  => '稽核时间',
                ],
                [
                    'columnName' => '稽核员',
                    'aliasName'  => '稽核员',
                ],
                [
                    'columnName' => '稽核项目',
                    'aliasName'  => '稽核项目',
                ],
                [
                    'columnName' => '稽核依据',
                    'aliasName'  => '稽核依据',
                ],
                [
                    'columnName' => '稽核结果',
                    'aliasName'  => '稽核结果',
                ],
                [
                    'columnName' => '存在问题',
                    'aliasName'  => '存在问题',
                ],
                [
                    'columnName' => '通知整改日期',
                    'aliasName'  => '通知整改日期',
                ],
                [
                    'columnName' => '整改责任人',
                    'aliasName'  => '整改责任人',
                ],
                [
                    'columnName' => '整改措施',
                    'aliasName'  => '整改措施',
                ],
                [
                    'columnName' => '反馈措施日期',
                    'aliasName'  => '反馈措施日期',
                ],
                [
                    'columnName' => '整改结果',
                    'aliasName'  => '整改结果',
                ],
                [
                    'columnName' => '完成情况',
                    'aliasName'  => '完成情况',
                ],
                [
                    'columnName' => '整改次数',
                    'aliasName'  => '整改次数',
                ],
                [
                    'columnName' => '整改验证人',
                    'aliasName'  => '整改验证人',
                ],
                [
                    'columnName' => '整改验证日期',
                    'aliasName'  => '整改验证日期',
                ],
                [
                    'columnName' => '关闭日期',
                    'aliasName'  => '关闭日期',
                ],
                [
                    'columnName' => '关闭人',
                    'aliasName'  => '关闭人',
                ],
                [
                    'columnName' => '状态',
                    'aliasName'  => '状态',
                ],
            ],
            '验证未过', '验证通过' => [
                [
                    'columnName' => '稽核日期',
                    'aliasName'  => '稽核日期',
                ],
                [
                    'columnName' => '单据编号',
                    'aliasName'  => '单据编号',
                ],
                [
                    'columnName' => '稽核级别',
                    'aliasName'  => '稽核级别',
                ],
                [
                    'columnName' => '受稽核对象',
                    'aliasName'  => '受稽核对象',
                ],
                [
                    'columnName' => '稽核类别',
                    'aliasName'  => '稽核类别',
                ],
                [
                    'columnName' => '稽核组长/稽核单位',
                    'aliasName'  => '稽核组长/稽核单位',
                ],
                [
                    'columnName' => '稽核时间',
                    'aliasName'  => '稽核时间',
                ],
                [
                    'columnName' => '稽核员',
                    'aliasName'  => '稽核员',
                ],
                [
                    'columnName' => '稽核项目',
                    'aliasName'  => '稽核项目',
                ],
                [
                    'columnName' => '稽核依据',
                    'aliasName'  => '稽核依据',
                ],
                [
                    'columnName' => '稽核结果',
                    'aliasName'  => '稽核结果',
                ],
                [
                    'columnName' => '存在问题',
                    'aliasName'  => '存在问题',
                ],
                [
                    'columnName' => '通知整改日期',
                    'aliasName'  => '通知整改日期',
                ],
                [
                    'columnName' => '整改责任人',
                    'aliasName'  => '整改责任人',
                ],
                [
                    'columnName' => '整改措施',
                    'aliasName'  => '整改措施',
                ],
                [
                    'columnName' => '反馈措施日期',
                    'aliasName'  => '反馈措施日期',
                ],
                [
                    'columnName' => '整改结果',
                    'aliasName'  => '整改结果',
                ],
                [
                    'columnName' => '完成情况',
                    'aliasName'  => '完成情况',
                ],
                [
                    'columnName' => '整改次数',
                    'aliasName'  => '整改次数',
                ],
                [
                    'columnName' => '整改验证人',
                    'aliasName'  => '整改验证人',
                ],
                [
                    'columnName' => '整改验证日期',
                    'aliasName'  => '整改验证日期',
                ],
                [
                    'columnName' => '状态',
                    'aliasName'  => '状态',
                ],
            ],
            '已反馈' => [
                [
                    'columnName' => '稽核日期',
                    'aliasName'  => '稽核日期',
                ],
                [
                    'columnName' => '单据编号',
                    'aliasName'  => '单据编号',
                ],
                [
                    'columnName' => '稽核级别',
                    'aliasName'  => '稽核级别',
                ],
                [
                    'columnName' => '受稽核对象',
                    'aliasName'  => '受稽核对象',
                ],
                [
                    'columnName' => '稽核类别',
                    'aliasName'  => '稽核类别',
                ],
                [
                    'columnName' => '稽核组长/稽核单位',
                    'aliasName'  => '稽核组长/稽核单位',
                ],
                [
                    'columnName' => '稽核时间',
                    'aliasName'  => '稽核时间',
                ],
                [
                    'columnName' => '稽核员',
                    'aliasName'  => '稽核员',
                ],
                [
                    'columnName' => '稽核项目',
                    'aliasName'  => '稽核项目',
                ],
                [
                    'columnName' => '稽核依据',
                    'aliasName'  => '稽核依据',
                ],
                [
                    'columnName' => '稽核结果',
                    'aliasName'  => '稽核结果',
                ],
                [
                    'columnName' => '存在问题',
                    'aliasName'  => '存在问题',
                ],
                [
                    'columnName' => '整改措施',
                    'aliasName'  => '整改措施',
                ],
                [
                    'columnName' => '通知整改日期',
                    'aliasName'  => '通知整改日期',
                ],
                [
                    'columnName' => '整改责任人',
                    'aliasName'  => '整改责任人',
                ],
                [
                    'columnName' => '反馈措施日期',
                    'aliasName'  => '反馈措施日期',
                ],
                [
                    'columnName' => '状态',
                    'aliasName'  => '状态',
                ],
            ],
            '已通知' => [
                [
                    'columnName' => '稽核日期',
                    'aliasName'  => '稽核日期',
                ],
                [
                    'columnName' => '单据编号',
                    'aliasName'  => '单据编号',
                ],
                [
                    'columnName' => '稽核级别',
                    'aliasName'  => '稽核级别',
                ],
                [
                    'columnName' => '受稽核对象',
                    'aliasName'  => '受稽核对象',
                ],
                [
                    'columnName' => '稽核类别',
                    'aliasName'  => '稽核类别',
                ],
                [
                    'columnName' => '稽核组长/稽核单位',
                    'aliasName'  => '稽核组长/稽核单位',
                ],
                [
                    'columnName' => '稽核时间',
                    'aliasName'  => '稽核时间',
                ],
                [
                    'columnName' => '稽核员',
                    'aliasName'  => '稽核员',
                ],
                [
                    'columnName' => '稽核项目',
                    'aliasName'  => '稽核项目',
                ],
                [
                    'columnName' => '稽核依据',
                    'aliasName'  => '稽核依据',
                ],
                [
                    'columnName' => '稽核结果',
                    'aliasName'  => '稽核结果',
                ],
                [
                    'columnName' => '存在问题',
                    'aliasName'  => '存在问题',
                ],
                [
                    'columnName' => '通知整改日期',
                    'aliasName'  => '通知整改日期',
                ],
                [
                    'columnName' => '整改责任人',
                    'aliasName'  => '整改责任人',
                ],
                [
                    'columnName' => '状态',
                    'aliasName'  => '状态',
                ],
            ],
            default => [
                [
                    'columnName' => '稽核日期',
                    'aliasName'  => '稽核日期',
                ],
                [
                    'columnName' => '单据编号',
                    'aliasName'  => '单据编号',
                ],
                [
                    'columnName' => '稽核级别',
                    'aliasName'  => '稽核级别',
                ],
                [
                    'columnName' => '受稽核对象',
                    'aliasName'  => '受稽核对象',
                ],
                [
                    'columnName' => '稽核类别',
                    'aliasName'  => '稽核类别',
                ],
                [
                    'columnName' => '稽核组长/稽核单位',
                    'aliasName'  => '稽核组长/稽核单位',
                ],
                [
                    'columnName' => '稽核时间',
                    'aliasName'  => '稽核时间',
                ],
                [
                    'columnName' => '稽核员',
                    'aliasName'  => '稽核员',
                ],
                [
                    'columnName' => '稽核项目',
                    'aliasName'  => '稽核项目',
                ],
                [
                    'columnName' => '稽核依据',
                    'aliasName'  => '稽核依据',
                ],
                [
                    'columnName' => '稽核结果',
                    'aliasName'  => '稽核结果',
                ],
                [
                    'columnName' => '存在问题',
                    'aliasName'  => '存在问题',
                ],
                [
                    'columnName' => '状态',
                    'aliasName'  => '状态',
                ],
            ]
        };
    }
}
